﻿namespace XGameLibrary.Controls
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    using XGameLibrary.Input;

    public class LeftRightSelector : Control
    {
        private Texture2D leftTexture;
        private Texture2D rightTexture;
        private Texture2D stopTexture;

        private List<string> items;

        private int maxItemWidth;
        private int selectedItem;

        public LeftRightSelector(Texture2D leftArrow, Texture2D rightArrow, Texture2D stop)
        {
            this.leftTexture = leftArrow;
            this.rightTexture = rightArrow;
            this.stopTexture = stop;
            this.TabStop = true;
            this.Color = Color.White;
            this.SelectedColor = Color.Red;
            this.items = new List<string>();
        }

        public event EventHandler SelectionChanged;

        public Color SelectedColor { get; set; }

        public int SelectedIndex
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                this.selectedItem = (int)MathHelper.Clamp(value, 0f, this.Items.Count);
            }
        }

        public string SelectedItem
        {
            get
            {
                return this.Items[this.selectedItem];
            }
        }

        public List<string> Items
        {
            get { return this.items; }
        }

        public void SetItems(string[] items, int maxWidth)
        {
            this.items.Clear();

            foreach (string str in items)
            {
                this.items.Add(str);
            }

            this.maxItemWidth = maxWidth;
        }

        protected void OnSelectionChanged()
        {
            if (this.SelectionChanged != null)
            {
                this.SelectionChanged(this, null);
            }
            Control.Select.Play();
        }

        public override void Update(GameTime gameTime)
        {
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 drawTo = this.Position;

            spriteBatch.Draw(
                this.selectedItem != 0 ? this.leftTexture : this.stopTexture,
                drawTo,
                Color.White);

            drawTo.X += this.leftTexture.Width + 5f;

            float itemWidth = SpriteFont.MeasureString(this.items[this.selectedItem]).X;
            float offset = (this.maxItemWidth - itemWidth) / 2;

            drawTo.X += offset;

            spriteBatch.DrawString(
                this.SpriteFont,
                this.items[this.selectedItem],
                drawTo,
                this.HasFocus ? this.SelectedColor : this.Color);

            drawTo.X += -1 * offset + this.maxItemWidth + 5f;

            spriteBatch.Draw(
                this.selectedItem != this.items.Count - 1 ? this.rightTexture : this.stopTexture,
                drawTo,
                Color.White);
        }

        public override void HandleInput()
        {
            if (this.items.Count == 0)
            {
                return;
            }

            if (InputHandler.KeyReleased(Keys.Left))
            {
                this.selectedItem--;
                if (this.selectedItem < 0)
                {
                    selectedItem = 0;
                }
                this.OnSelectionChanged();
            }

            if (InputHandler.KeyReleased(Keys.Right))
            {
                this.selectedItem++;
                if (this.selectedItem >= this.items.Count)
                {
                    this.selectedItem = this.items.Count - 1;
                }
                this.OnSelectionChanged();
            }
        }
    }
}
